package com.example.membersteward.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.membersteward.common.CommonResult;
import com.example.membersteward.entity.RoleMenu;
import com.example.membersteward.exception.ServiceException;
import com.example.membersteward.service.RoleMenuService;
import com.example.membersteward.mapper.RoleMenuMapper;
import com.example.membersteward.util.BaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 26690
 * @description 针对表【role_menu(角色与菜单关系表)】的数据库操作Service实现
 * @createDate 2025-04-08 22:52:37
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
        implements RoleMenuService {

    private static final Logger logger = LoggerFactory.getLogger(RoleMenuServiceImpl.class);
    @Resource
    RoleMenuMapper roleMenuMapper;

    @Override
    public CommonResult getMenuIdsByRoleId(Long roleId) {
        try {
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", roleId);
            List<Map<String, Object>> maps = roleMenuMapper.selectMaps(queryWrapper);

            return CommonResult.success(maps);
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }

    @Override
    @Transactional
    public CommonResult assignPermission(Long roleId, List<Long> menuIds) {
        try {
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", roleId);
            roleMenuMapper.delete(queryWrapper);
            if (BaseUtils.isNotEmpty(menuIds)) {
                roleMenuMapper.insertRoleUser(roleId, menuIds);
            }

            return CommonResult.success("执行成功");
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }
}




